package uit.edu.vn.Utility;

import java.io.Reader;
import java.util.ArrayList;

import uit.edu.vn.Contact.Address;
import uit.edu.vn.Contact.Email;
import uit.edu.vn.Contact.IM;
import uit.edu.vn.Contact.MyContact;
import uit.edu.vn.Contact.Organization;
import uit.edu.vn.Contact.Phone;
import uit.edu.vn.Model.mySMS;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class xStreamXML {

	public static String contactToXML(ArrayList<MyContact> contactList) {

		XStream writer = new XStream();
		writer.alias("contact", MyContact.class);
		writer.aliasAttribute(MyContact.class, "checked", "checked");
		writer.aliasAttribute(MyContact.class, "id", "ID");
		writer.aliasAttribute(MyContact.class, "displayName", "name");
		writer.aliasField("notes", MyContact.class, "notes");
		
		writer.alias("phone", Phone.class);
		writer.aliasAttribute(Phone.class, "number", "value");
		writer.aliasAttribute(Phone.class, "type", "type");
		
		writer.alias("email", Email.class);
		writer.aliasAttribute(Email.class, "address", "value");
		writer.aliasAttribute(Email.class, "type", "type");
		
		writer.alias("address", Address.class);
		writer.aliasAttribute(Address.class, "poBox", "pobox");
		writer.aliasAttribute(Address.class, "street", "street");
		writer.aliasAttribute(Address.class, "city", "city");
		writer.aliasAttribute(Address.class, "state", "state");
		writer.aliasAttribute(Address.class, "postalCode", "postal_code");
		writer.aliasAttribute(Address.class, "country", "country");
		writer.aliasAttribute(Address.class, "type", "type");
		
		writer.alias("IM", IM.class);
		writer.aliasAttribute(IM.class, "name", "name");
		writer.aliasAttribute(IM.class, "type", "type");
		
		writer.alias("organization", Organization.class);
		writer.aliasAttribute(Organization.class, "organization", "org");
		writer.aliasAttribute(Organization.class, "positions", "pos");
		writer.aliasAttribute(Organization.class, "type", "type");

		return writer.toXML(contactList);
	}

	@SuppressWarnings("unchecked")
	public static ArrayList<MyContact> fromXML(String xml) {
		XStream reader = new XStream(new DomDriver());
		reader.alias("contact", MyContact.class);
		reader.aliasAttribute(MyContact.class, "checked", "checked");
		reader.aliasAttribute(MyContact.class, "id", "ID");
		reader.aliasAttribute(MyContact.class, "displayName", "name");
		reader.aliasField("notes", MyContact.class, "notes");
		
		reader.alias("phone", Phone.class);
		reader.aliasAttribute(Phone.class, "number", "value");
		reader.aliasAttribute(Phone.class, "type", "type");
		
		reader.alias("email", Email.class);
		reader.aliasAttribute(Email.class, "address", "value");
		reader.aliasAttribute(Email.class, "type", "type");
		
		reader.alias("address", Address.class);
		reader.aliasAttribute(Address.class, "poBox", "pobox");
		reader.aliasAttribute(Address.class, "street", "street");
		reader.aliasAttribute(Address.class, "city", "city");
		reader.aliasAttribute(Address.class, "state", "state");
		reader.aliasAttribute(Address.class, "postalCode", "postal_code");
		reader.aliasAttribute(Address.class, "country", "country");
		reader.aliasAttribute(Address.class, "type", "type");
		
		reader.alias("IM", IM.class);
		reader.aliasAttribute(IM.class, "name", "name");
		reader.aliasAttribute(IM.class, "type", "type");
		
		reader.alias("organization", Organization.class);
		reader.aliasAttribute(Organization.class, "organization", "org");
		reader.aliasAttribute(Organization.class, "positions", "pos");
		reader.aliasAttribute(Organization.class, "type", "type");

		return (ArrayList<MyContact>) reader.fromXML(xml);
	}

	@SuppressWarnings("unchecked")
	public static ArrayList<MyContact> fromXML(Reader xml) {
		XStream reader = new XStream(new DomDriver());
//		XStream reader = new XStream();
		reader.alias("contact", MyContact.class);
		reader.aliasAttribute(MyContact.class, "checked", "checked");
		reader.aliasAttribute(MyContact.class, "id", "ID");
		reader.aliasAttribute(MyContact.class, "displayName", "name");
		reader.aliasField("notes", MyContact.class, "notes");
		
		reader.alias("phone", Phone.class);
		reader.aliasAttribute(Phone.class, "number", "value");
		reader.aliasAttribute(Phone.class, "type", "type");
		
		reader.alias("email", Email.class);
		reader.aliasAttribute(Email.class, "address", "value");
		reader.aliasAttribute(Email.class, "type", "type");
		
		reader.alias("address", Address.class);
		reader.aliasAttribute(Address.class, "poBox", "pobox");
		reader.aliasAttribute(Address.class, "street", "street");
		reader.aliasAttribute(Address.class, "city", "city");
		reader.aliasAttribute(Address.class, "state", "state");
		reader.aliasAttribute(Address.class, "postalCode", "postal_code");
		reader.aliasAttribute(Address.class, "country", "country");
		reader.aliasAttribute(Address.class, "type", "type");
		
		reader.alias("IM", IM.class);
		reader.aliasAttribute(IM.class, "name", "name");
		reader.aliasAttribute(IM.class, "type", "type");
		
		reader.alias("organization", Organization.class);
		reader.aliasAttribute(Organization.class, "organization", "org");
		reader.aliasAttribute(Organization.class, "positions", "pos");
		reader.aliasAttribute(Organization.class, "type", "type");

		return (ArrayList<MyContact>) reader.fromXML(xml);
	}
	
	//-----------------------sms------------------------------
	public static String smsToXML(ArrayList<mySMS> contactList) {

		XStream writer = new XStream();
		writer.alias("sms", mySMS.class);
		writer.aliasAttribute(mySMS.class, "check", "checked");
		writer.aliasAttribute(mySMS.class, "id", "ID");
		writer.aliasAttribute(mySMS.class, "thread_id", "threadId");
		writer.aliasAttribute(mySMS.class, "smsNumber", "number");
		writer.aliasAttribute(mySMS.class, "smsName", "name");
		writer.aliasAttribute(mySMS.class, "smsBody", "body");
		writer.aliasAttribute(mySMS.class, "smsDate", "date");
		writer.aliasAttribute(mySMS.class, "smsProtocol", "protocol");
		writer.aliasAttribute(mySMS.class, "smsType", "type");
		writer.aliasAttribute(mySMS.class, "currentDay", "currentDay");
		
		writer.aliasField("notes", mySMS.class, "notes");
				return writer.toXML(contactList);
	}

	@SuppressWarnings("unchecked")
	public static ArrayList<mySMS> smsfromXML(String xml) {
		XStream reader = new XStream(new DomDriver());
		reader.alias("sms", mySMS.class);
		reader.aliasAttribute(mySMS.class, "check", "checked");
		reader.aliasAttribute(mySMS.class, "id", "ID");
		reader.aliasAttribute(mySMS.class, "thread_id", "threadId");
		reader.aliasAttribute(mySMS.class, "smsNumber", "address");
		reader.aliasAttribute(mySMS.class, "smsName", "name");
		reader.aliasAttribute(mySMS.class, "smsBody", "body");
		reader.aliasAttribute(mySMS.class, "smsDate", "date");
		reader.aliasAttribute(mySMS.class, "smsProtocol", "protocol");
		reader.aliasAttribute(mySMS.class, "smsType", "type");
		reader.aliasAttribute(mySMS.class, "currentDay", "currentday");
		reader.aliasField("notes", mySMS.class, "notes");

		return (ArrayList<mySMS>) reader.fromXML(xml);
	}

	@SuppressWarnings("unchecked")
	public static ArrayList<mySMS> smsfromXML(Reader xml) {
		XStream reader = new XStream(new DomDriver());
		reader.alias("sms", mySMS.class);
		reader.aliasAttribute(mySMS.class, "check", "checked");
		reader.aliasAttribute(mySMS.class, "id", "ID");
		reader.aliasAttribute(mySMS.class, "thread_id", "threadId");
		reader.aliasAttribute(mySMS.class, "smsNumber", "address");
		reader.aliasAttribute(mySMS.class, "smsName", "name");
		reader.aliasAttribute(mySMS.class, "smsBody", "body");
		reader.aliasAttribute(mySMS.class, "smsDate", "date");
		reader.aliasAttribute(mySMS.class, "smsProtocol", "protocol");
		reader.aliasAttribute(mySMS.class, "smsType", "type");
		reader.aliasAttribute(mySMS.class, "currentDay", "currentday");
		reader.aliasField("notes", mySMS.class, "notes");

		return (ArrayList<mySMS>) reader.fromXML(xml);
	}
}
